<script>
import store from "@/store/index";
export default {
  onShow () {
    var that=this
    var uat = wx.getStorageSync("uat")
    console.log(uat,'uatuatuat')
    if(uat!=''&&uat!=null&&uat!=undefined){       
        that.$api.user.getUserInfo().then(res=>{
          if(res.status===0){
               that.$store.commit('getUat',uat);
          }else{
            wx.setStorageSync("uat")
            that.$store.commit('getUat','');
          }
            console.log(res)
        })
    }
    
    //判断是否链接网路
     this.lookFile()
     this.getOpenId()
     // 查看是否授权
      wx.getSetting({
        success: function(res){
          if (res.authSetting['scope.userInfo']) {
            // 已经授权，可以直接调用 getUserInfo 获取头像昵称
            console.log("已授权")
            that.$store.commit('getIsAuthorization',2);
            that.getUserInfo()
          }else{
            console.log("未授权")
            that.$store.commit('getIsAuthorization',1);
          }
        }
      })

    
  },
  methods:{
    // 预览文档
    lookFile(){
      wx.getNetworkType({
        success: function (res) {
          if (res.networkType == 'none') {
                wx.showToast({
                  title:'无法连接网络，请重新尝试'
                })
          }
        },
        fail(res) {
            console.log("获取用户信息失败", res)
          }
      })
    },
    //获取用户信息
    getUserInfo(){
      var that=this
      wx.getUserInfo({
          success: function (res) { 
              console.log(res);
              that.$store.commit('getWxUserInfo',res.userInfo);
          }
      })
    },
    //获取openID
    getOpenId() {
      var that=this
      wx.login({
        success: function(res) {
          if (res.code) {
            var dataCode={
              code:res.code
            }
            console.log(dataCode)
            return false
            that.$api.user.WXauthorization(dataCode).then(row=>{  
                if(row.status===0){
                  if(row.data!=null&&row.data!=''){
                    that.$store.commit('getOpenid',row.data.openid);     
                  }                              
                }else{
                  wx.showToast({
                    title: row.message,
                    icon: "none",
                    duration: 1000
                  });
                }
                that.common.hideToast();
            })
          }else{
            wx.showToast({
              title: row.message,
              icon: "none",
              duration: 1000
            });
          }
        }
      });
    }, 
  }
}
</script>

<style>
page{
  background:#f5f5f5;
}
.container {
  height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: space-between;
  padding: 200rpx 0;
  box-sizing: border-box;
}
/* this rule will be remove */
* {
  transition: width 2s;
  -moz-transition: width 2s;
  -webkit-transition: width 2s;
  -o-transition: width 2s;
}
.fl{
  float: left;
}
.fr{
  float: right;;
}
.clearfix:after{
  content:"";
  height:0;
  line-height:0;
  display:block;
  visibility:hidden;
  clear:both
}
.clearfix{
  zoom:1;
}
</style>
